home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / ccmd / cmtok.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-19  |  3.5 KB  |  121 lines

  1. /*
  2.  Author: Andrew Lowry
  3.  
  4.  Columbia University Center for Computing Activities, July 1986.
  5.  Copyright (C) 1986, 1987, Trustees of Columbia University in the City
  6.  of New York.  Permission is granted to any individual or institution
  7.  to use, copy, or redistribute this software so long as it is not sold
  8.  for profit, provided this copyright notice is retained.
  9. */
  10. /* cmtok
  11. **
  12. ** This module contains handlers for parsing arbitrary token strings.
  13. ** A parse succeeds if the current input exactly matches the given
  14. ** token, with case insignificant in letters.  No terminating character
  15. ** is needed for a successful parse.  Completion succeeds if any
  16. ** prefix of the token has been typed.  Full completion adds a space
  17. ** after the token as well.  Partial completion will stop after the
  18. ** first punctuation char completed. Help text echoes the expected string.
  19. ** The standard break table breaks on every single character, 
  20. ** so CM_WKF parsing will work correctly (since no terminating
  21. ** character is required).
  22. **/
  23.  
  24. #define    TOKERR            /* token error table allocated here */
  25.  
  26. #include "ccmdlib.h"            /* get standard symbols */
  27. #include "cmfncs.h"        /* and internal symbols */
  28.  
  29. /* Forward declaration of handler routines */
  30.  
  31. int tokprs(), tokhlp(), tokcplt();
  32.         
  33. #define    tokbrk    cmallbk        /* std break table breaks on everything */
  34.  
  35. ftspec ft_tok = { tokprs, tokhlp, tokcplt, 0, &tokbrk };
  36.  
  37. /* tokprs
  38. **
  39. ** Purpose:
  40. **   Attempt to parse the given token string.  If the string is
  41. **   not completely present in the input, signal an incomplete
  42. **   parse.  If there are any mismatched characters, signal an
  43. **   error.
  44. **/
  45.  
  46. PASSEDSTATIC int
  47. tokprs(text,textlen,fdbp,parselen,value)
  48. char *text;
  49. int textlen;
  50. fdb *fdbp;
  51. int *parselen;
  52. pval *value;
  53. {
  54.   char *tok = (char *) fdbp->_cmdat;    /* point to token string */
  55.   char c1,c2;                /* chars in token and text */
  56.   int toklen;                /* length of token string */
  57.   
  58.   toklen = 0;                /* haven't parsed any chars yet */
  59.   while ((c1 = *tok++) != NULCHAR) {    /* loop to end of token string */
  60.     if (++toklen > textlen)        /* ran out of input? */
  61.       return(CMxINC);            /* ok, good match up to here */
  62.     c2 = (*text++) & CC_CHR;        /* get next input char */
  63.     if (islower(c1))            /* convert both chars to upper case */
  64.       c1 = toupper(c1);
  65.     if (islower(c2))
  66.       c2 = toupper(c2);
  67.     if (c1 != c2)
  68.       return(TOKxNP);            /* mismatch */
  69.   }
  70.   *parselen = toklen;            /* good match - set parse length */
  71.   value->_pvstr = cmcsb._cmabp;        /* return value in atom buffer */
  72.   if (toklen == textlen)
  73.       if (!(fdbp->_cmffl & TOK_WAK))
  74.       return(CMxINC);
  75.   return(CMxOK);
  76. }
  77.  
  78.  
  79.  
  80. /* tokhlp - Identify the string we're looking for */
  81.  
  82. PASSEDSTATIC int
  83. tokhlp(text,textlen,fdbp,cust)
  84. char *text;
  85. int textlen,cust;
  86. fdb *fdbp;
  87. {
  88.   if (!cust)
  89.     cmxputs("matching token: ");    /* start the message if they didnt */
  90.   cmxputs((char *) fdbp->_cmdat);    /* then print the token string */
  91.   return(CMxOK);
  92. }
  93.  
  94.  
  95.  
  96.  
  97. /* tokcplt - Finish off the token string for them.  Add a space if
  98. ** this is full completion.
  99. **/
  100.  
  101. PASSEDSTATIC int
  102. tokcplt(text,textlen,fdbp,full,cplt,cpltlen)
  103. char *text,**cplt;
  104. int textlen,full,*cpltlen;
  105. fdb *fdbp;
  106. {
  107.   char *tok = (char *) fdbp->_cmdat;    /* point to token string */
  108.   char c1,c2;                /* chars in token and text */
  109.   
  110.   if (textlen == 0) {
  111.     *cplt = NULL;            /* beep on no text */
  112.     return(CMP_BEL);
  113.   }
  114.   *cplt = tok + textlen;        /* set rest of token for completion */
  115.   *cpltlen = -1;            /* complete to end of string */
  116.   if (full)
  117.     return(CMP_SPC | CMP_GO);        /* add space and wakeup for full */
  118.   else
  119.     return(CMP_PNC);            /* only up to punct for partial */
  120. }
  121.